package com.syni.mdd.yhd.common.all.entity;

import cn.hutool.core.date.StopWatch;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
 * @className Main
 * @description TOO
 * @Author cfx
 * @DATE 2020/3/11 19:28
 * @VERSION 1.0
 **/
public class Main {
    public static void main(String[] args) {
        /*Executor executor = Executors.newFixedThreadPool(100);
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        List<String> userIds=new ArrayList<>();
        userIds.add("123");
        userIds.add("124");
        userIds.add("125");
        userIds.add("126");
        List<CompletableFuture<UserInfo>>  userInfoList=userIds.stream().map((s)->{
            return CompletableFuture.supplyAsync(()->{
                UserService userService=new UserService();
                return userService.queryUserInfoById(s);
            },executor);
        }).collect(Collectors.toList());

        CompletableFuture<Void> allFutures=CompletableFuture.allOf(userInfoList.toArray(new CompletableFuture[userInfoList.size()]));

        CompletableFuture<List<UserInfo>> allFutureList=allFutures.thenApply(v->{
            return userInfoList.stream().map(a -> {
                try {
                    //return a.get(10,TimeUnit.MILLISECONDS);
                    return a.join();
                } catch (Exception e) {
                }
                return null;
            }).collect(Collectors.toList());
        });
        List<UserInfo> list = allFutureList.join();
        for(UserInfo u:list){
            System.out.println(u.getNickName()+"--"+u.getUserId()+"--"+u.getUserName());
        }
        stopWatch.stop();
        long t = stopWatch.getTotalTimeMillis();
        System.out.println(t);*/
        test();

        /*String s = "applepenapple";
        List<String> wordDict = new ArrayList<>();
        wordDict.add("apple");
        wordDict.add("pen");*/
        //System.out.println(wordBreak(s,wordDict));
    }

    public static void test(){
        ExecutorService executor = Executors.newFixedThreadPool(1);
        Future<String> future = executor.submit(() -> {
            // long running task
            Thread.sleep(10000);
            return "task finish.";
        });
        try {
            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println("会先执行？");
    }


    /*public static boolean wordBreak(String s, List<String> wordDict) {
        boolean ret = false;
        if(wordDict.size() > 0) {
            for(String word : wordDict) {
                if (word.length() <= s.length()) {
                    while (true) {
                        String split;
                        for (String words : wordDict) {
                            split = s.substring(0, words.length());
                            if (wordDict.contains(split)) {
                                s = s.substring(split.length());
                                break;
                            }
                        }
                    }
                        for (String words : wordDict) {
                            String split = s.substring(0, word.length());
                            if (wordDict.contains(split)) {
                                s = s.substring(split.length());
                            } else {
                                break;
                            }
                        }
                    }

                }

            }
                }for (String word : wordDict) {
                    if (word.length() <= sub.length()) {
                        String split = s.substring(0, word.length());
                        if (wordDict.contains(split)) {
                            s = s.substring(word.length());
                        }
                    }
                }
            }


        }
        return ret;
    }*/
}
